//
//  WBVisitorView.swift
//  sinaWeibo
//
//  Created by 吴熠 on 16/11/13.
//  Copyright © 2016年 吴熠. All rights reserved.
//

import UIKit
import SnapKit

//使用代理传递点击事件的消息
@objc protocol WBVisitorLoginRegisterDelegate : NSObjectProtocol{
    //代理方法
    //用户将要登录
   @objc optional func userWillLogin()
    
    //用户将要注册
    func userWillRegister()
    
    
}


class WBVisitorView: UIView {

    //代理属性,用weak关键字来修饰
    weak var delegate : WBVisitorLoginRegisterDelegate?
    
    //根据点击不同的item更新引导界面的信息
    func upDateUI(title:String,imageName:String,isHome:Bool = false){
        self.tipLable.text = title
        self.circileView.image = UIImage(named: imageName)
        if isHome {
            //设置动画
            startAnimation()
        }else
        {
            //隐藏首页中的大房子和遮罩的view
            self.houseView.isHidden = true
            self.backView.isHidden = true
        }
    }
    //circleView旋转的动画
    private func startAnimation(){
        let animator = CABasicAnimation(keyPath: "transform.rotation")
        
        //设置动画
        animator.repeatCount = MAXFLOAT
        animator.toValue = M_PI*2
        animator.duration = 20
        //动画开启之后不移除
        animator.isRemovedOnCompletion = false
        circileView.layer.add(animator, forKey: nil)
    }
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //点击事件的方法
    @objc private func loginBtnDidClick(){
        //响应代理方法,对应是可选还是必选的协议方法
        delegate?.userWillLogin?()//userWillLogin是定义的可选方法用?修饰
    }
    @objc private func registerBtnDidClick(){
        delegate?.userWillRegister()
    }
    
    private func setupUI(){
        self.addSubview(circileView)
        self.addSubview(backView)
        self.addSubview(houseView)
        self.addSubview(tipLable)
        self.addSubview(loginBtn)
        self.addSubview(registerBtn)
        
        //添加button点击事件
        loginBtn.addTarget(self, action: #selector(loginBtnDidClick), for: .touchUpInside)
        registerBtn.addTarget(self, action: #selector(registerBtnDidClick), for: .touchUpInside)
        
        //设置背景颜色
        self.backgroundColor = UIColor.init(white: 237.0/255.0, alpha: 1)
        
        //设置约束
        circileView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        houseView.snp.makeConstraints { (make) in
            make.center.equalTo(circileView)
        }
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(circileView)
        }
        tipLable.snp.makeConstraints { (make) in
            make.centerX.equalTo(circileView)
            make.top.equalTo(circileView.snp.bottom)
        }
        loginBtn.snp.makeConstraints { (make) in
            make.left.equalTo(tipLable.snp.left)
            make.width.equalTo(100)
            make.top.equalTo(tipLable.snp.bottom).offset(20)
        }
        registerBtn.snp.makeConstraints { (make) in
            make.right.equalTo(tipLable.snp.right)
            make.width.equalTo(100)
            make.top.equalTo(tipLable.snp.bottom).offset(20)
        }

        
    }
    
    //实例化控件----圈圈,大房子,引导标签,登录按钮,注册按钮
    private lazy var circileView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    private lazy var houseView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    private lazy var backView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var tipLable : UILabel = {
        
        let tip = UILabel(title: "我追你,我追到你,你就让我,嘿~嘿~嘿~", fontSize: 14, textColor: UIColor.darkGray)
        
        tip.numberOfLines = 0
        tip.textAlignment = .center
        
        tip.preferredMaxLayoutWidth = 220
        
        return tip
    }()
    //登录和注册按钮
    private lazy var loginBtn : UIButton = {
        let btn = UIButton(title: "登录", titleColor: UIColor.darkGray, fontSize: 14)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        
        return btn
    }()
    
    private lazy var registerBtn : UIButton = {
        let btn = UIButton(title: "注册", titleColor: UIColor.orange, fontSize: 14)
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        
        return btn
    }()
    
}
